﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Collections;

namespace Acsys.Entity
{
    public class CreditType : IEntity
    {
        public int ID = 0;
        public string CODE = "D00000";
        public string NAME = "";
        public bool ISPARENT = false;
        public CreditType PARENT = null;

        public CreditType()
        {
        }
        public CreditType(int id)
        {
            ID = id;
        }
        public CreditType(int id, string code)
        {
            ID = id;
            CODE = code;
        }
        public IEntity Get(MySqlDataReader aReader)
        {
            CreditType trtype = null;
            while (aReader.Read())
            {
                trtype = new CreditType();
                trtype.ID = Convert.ToInt32(aReader[0]);
                trtype.CODE = aReader[1].ToString();
                trtype.NAME = aReader[2].ToString();
                trtype.ISPARENT = Convert.ToBoolean(aReader[3]);
                trtype.PARENT = Convert.ToInt32(aReader[4]) == 0 ? null : new CreditType(Convert.ToInt32(aReader[4]));
            }
            aReader.Close();
            return trtype;
        }
        public string GetInsertSQL()
        {
            return String.Format(@"insert into table_transactiontype 
                (crty_code,crty_name,crty_isparent,crty_parent) 
                VALUES ('{0}','{1}',{2},{3})",
                                             CODE, NAME, Convert.ToByte(ISPARENT), PARENT == null ? 0 : PARENT.ID);
        }
        public string GetDeleteSQL()
        {
            return "delete from table_transactiontype where crty_id = " + ID;
        }
        public string GetUpdateSQL()
        {
            return String.Format(@"update table_transactiontype set 
                crty_code = '{0}', 
                crty_name='{1}',
                crty_isparent='{2}',
                crty_parent={3}
                where crty_id = {4}",
                CODE, NAME, Convert.ToByte(ISPARENT), PARENT == null ? 0 : PARENT.ID, ID);
        }
        public string GetByIDSQL(int ID)
        {
            return String.Format("select * from table_transactiontype where crty_id = {0}", ID);
        }
        public string GetByCodeSQL(string code)
        {
            return String.Format("select * from table_transactiontype where crty_code = '{0}'", code);
        }
        public string GetByCodeLikeSQL(string text)
        {
            return String.Format("select * from table_transactiontype where crty_code like '%{0}%'", text);
        }
        public string GetByNameLikeSQL(string text)
        {
            return String.Format("select * from table_transactiontype where crty_name like '%{0}%'", text);
        }
        public string GetAllSQL()
        {
            return String.Format("select * from table_transactiontype order by crty_code");
        }
        public string GetAllParentSQL()
        {
            return String.Format("select * from table_transactiontype where crty_isparent='1'");
        }
        public string GetAllChildSQL()
        {
            return String.Format("select * from table_transactiontype where crty_isparent='0'");
        }
        public string GetAllChildSQL(int parentID)
        {
            return String.Format("select * from table_transactiontype where crty_parent={0}", parentID);
        }
        public IList GetAll(MySqlDataReader aReader)
        {
            IList result = new ArrayList();
            while (aReader.Read())
            {
                CreditType trtype = new CreditType();
                trtype.ID = Convert.ToInt32(aReader[0]);
                trtype.CODE = aReader[1].ToString();
                trtype.NAME = aReader[2].ToString();
                trtype.ISPARENT = Convert.ToBoolean(aReader[3]);
                trtype.PARENT = Convert.ToInt32(aReader[4]) == 0 ? null : new CreditType(Convert.ToInt32(aReader[4]));
                result.Add(trtype);
            }
            aReader.Close();
            return result;
        }
        public int GetID()
        {
            return ID;
        }
        public void SetID(int id)
        {
            ID = id;
        }
        public override string ToString()
        {
            return CODE +" - " + NAME;
        }
        public string GetCode()
        {
            return CODE;
        }
        public void SetCode(string code)
        {
            CODE = code;
        }
        public string GetMaximumIDSQL()
        {
            return String.Format("SELECT max(crty_id) from table_transactiontype");
        }

    }
}
